package com.stylefeng.guns.core.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.modular.system.model.Dict;
import com.stylefeng.guns.modular.system.service.IDictService;

import java.util.List;


/**
 * 获取字典信息
 */
public class DictUtil {

    IDictService iDictService =SpringContextHolder.getBean(IDictService.class);

    /**
     * 根据字典code和字典中的值获取对应的名称
     */
    public String getDictsByCode(String code) {
        StringBuffer html = new StringBuffer();
        html.append("<option value=''>-请选择-</option>\r\n");
        //组装查询
        Wrapper<Dict> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("code", code);
        Dict dict = iDictService.selectOne(wrapper);
        if (dict == null) {
            return html.toString();
        } else {
            wrapper = new EntityWrapper<>();
            wrapper = wrapper.eq("pid", dict.getId());
            List<Dict> dicts = iDictService.selectList(wrapper);
            for (Dict item : dicts) {
                html.append("<option value=\""+item.getCode()+"\">"+item.getName()+"</option>\r\n");
            }
            return html.toString();
        }
    }
}
